//
//  UserInfoViewController.swift
//  HuaYun
//
//  Created by enghou on 2018/7/16.
//  Copyright © 2018年 xyxorigation. All rights reserved.
//

import UIKit

class UserInfoViewController: TitleContentTableViewController {
    
    override var names: Array<String> {
        return ["info"]
    }
    
    override func viewWillAppear(_ animated: Bool) {
        
        super.viewWillAppear(animated)
        
        self.navigationController?.navigationBar.isHidden = false
    }
    
    override func success(for name: String) -> NetSuccess{
        
        func suc(json: JSON){
            if let success = json.contents?["success"]?.rawValue as? Bool {
                if success == false {
                    self.alert(json.contents?["errorMsg"]?.rawString())
                    return
                }
            }
            var name: String = ""
            var phone: String = ""
            var belong: String = ""
            var occupy: String = ""
            if let data = json.contents?["data"] as JSON? {
                if let nm = data.contents?["name"]?.rawString() {
                    name = nm
                }
                if let phe = data.contents?["phone"]?.rawString() {
                    phone = phe
                }
                if let blong = data.contents?["orgName"]?.rawString() {
                    belong = blong
                }
                if let roleinfo = data.contents?["roleInfo"]?.rawString() {
                    occupy = roleinfo
                }
                self.source = [["姓名":name],["手机号码":phone],["归属部门":belong],["职务":occupy]]
                self.tableView.reloadData()
            }
            
        }
        
        return suc
    }
//
    override func param(for name: String) -> Dictionary<String,Any>{
        return [:]
    }
//
    override func path(for name: String) -> String{
        return GetUserInfo
    }

    override func method(for name: String) -> NetMethod{
        return .get
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        self.load(with: "info")
        self.source = [["姓名":""],["手机号码":""],["归属部门":""],["职务":""]]
        self.tableView.tableFooterView = UIView()
        self.tableView.reloadData()
    }
    
    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        
        var cell = tableView.dequeueReusableCell(withIdentifier: "Cell")
        
        if cell == nil {
            cell = UITableViewCell.init(style: .value1, reuseIdentifier: "Cell")
            cell?.textLabel?.font = UIFont.systemFont(ofSize: 15)
            cell?.textLabel?.textColor = UIColor.hex("333333")
            cell?.detailTextLabel?.font = UIFont.systemFont(ofSize: 15)
            cell?.detailTextLabel?.textColor = UIColor.hex("999999")
            cell?.selectionStyle = .none
        }
        
        let item = source[indexPath.row]
        
        cell?.textLabel?.text = item.keys.first
        
        cell?.detailTextLabel?.text = item[item.keys.first!] as? String
        
        return cell!
        
    }

    
    override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 50
    }
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

}
